package com.pcj.blog.web.admin;/**
 * @param
 * @author pcj
 * @date 2021/12/27 15:26
 * @return
 */

import com.pcj.blog.po.Blog;
import com.pcj.blog.po.User;
import com.pcj.blog.service.BlogService;
import com.pcj.blog.service.TagService;
import com.pcj.blog.service.TypeService;
import com.pcj.blog.vo.BlogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;


/**
 *
 * @className BlogController
 * @author pengcj
 * @date 2021/12/27 15:26
 */
@Controller
@RequestMapping("/admin")
public class BlogController {
    private final  String INPUT="admin/blog-input";
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    @RequestMapping("/blogs")
    private String list(Model model , @PageableDefault(size = 2,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable, BlogVo blog){
        model.addAttribute("types",typeService.listType());
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return "admin/blogs";
    }

    @GetMapping("/blogs/input")
    public String input(Model model){
        //初始化
        model.addAttribute("tags",tagService.listTag());
        model.addAttribute("types",typeService.listType());
        model.addAttribute("blog",new Blog());
        return INPUT;
    }
    @RequestMapping("/blogs/search")
    private String search(Model model , @PageableDefault(size = 2,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable, BlogVo blog){
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return "admin/blogs :: bloglist";//返回该页面下的  bloglist 的片段
    }
    //新增博客
    @PostMapping("/blogs")
    public String  addblog(Blog blog, RedirectAttributes redirectAttributes, HttpSession session){
        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getTypebyId(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getTagids()));
        Blog  b=blogService.saveBlog(blog);
        if(b==null){
            redirectAttributes.addFlashAttribute("message","操作失败");
        }else{
            redirectAttributes.addFlashAttribute("message","操作成功");
        }
        return "redirect:/admin/blogs";
    }
    //编辑博客
    @GetMapping("/blogs/{id}/input")
    public String editblog(Model model ,@PathVariable  Long id){
        //初始化
        model.addAttribute("tags",tagService.listTag());
        model.addAttribute("types",typeService.listType());
        Blog blog=blogService.getBlog(id);
        blog.init();  //  处理tag id
            model.addAttribute("blog",blog); //根据id获取博客内容 返回到发布博客页面
        return INPUT;
    }

    //删除博客
    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable  Long id,RedirectAttributes redirectAttributes){
        blogService.deleteBlog(id);
        redirectAttributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/blogs";
    }
}
